from flask import Blueprint, request, jsonify
from service.technician import TechnicianService

# 创建蓝图
technician_bp = Blueprint('technician', __name__, url_prefix='/master')

@technician_bp.route('/list', methods=['GET'])
def get_technician():
    # 获取单个师傅、所有师傅或按姓名搜索
    technician_id = request.args.get('id')
    name = request.args.get('name')

    if technician_id:
        return TechnicianService.get_technician_by_id(technician_id)
    elif name:
        return TechnicianService.get_technicians_by_name(name)
    else:
        return TechnicianService.get_all_technicians(request.args)

@technician_bp.route('/add', methods=['POST'])
def create_technician():
    # 创建新师傅
    data = request.get_json()
    if not data:
        return jsonify({'code': 10002, 'message': 'No input data provided', 'data': None}), 400
    return TechnicianService.create_technician(data)

@technician_bp.route('/edit', methods=['POST'])
def update_technician():
    # 更新师傅
    data = request.get_json()
    if not data or 'id' not in data:
        return jsonify({'code': 10002, 'message': 'Technician ID is required', 'data': None}), 400
    technician_id = data.pop('id')
    return TechnicianService.update_technician(technician_id, data)

@technician_bp.route('/delete', methods=['POST'])
def delete_technician():
    # 删除师傅
    technician_id = request.args.get('id')
    if not technician_id:
        return jsonify({'code': 10002, 'message': 'Technician ID is required', 'data': None}), 400
    return TechnicianService.delete_technician(technician_id)

@technician_bp.route('/login', methods=['POST'])
def login_technician():
    # 师傅登录
    data = request.get_json()
    if not data or 'phone' not in data or 'password' not in data:
        return jsonify({'code': 10002, 'message': 'Phone and password are required', 'data': None}), 400
    phone = data['phone']
    password = data['password']
    return TechnicianService.login_technician(phone, password)
